

package com.mt.mall.controller.User;

import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.User.ShippingAddressEditDto;
import com.mt.mall.entity.User.ShippingAddress;
import com.mt.mall.service.User.ShippingAddressService;
import com.mt.common.system.service.UserService;


@Api(tags = "收获地址")
@RestController
@RequestMapping("/api/mall/User/ShippingAddress")
@CrossOrigin(allowCredentials = "true")
public class ShippingAddressController {
    private static Logger logger = LoggerFactory.getLogger(ShippingAddressController.class);


    @Autowired
    private ShippingAddressService shippingAddressService;
    @Autowired
    private UserService userService;

    /**
     * 根据分页参数查询收获地址集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:view')")
    @ApiOperation("分页查询收获地址")
    @ApiPageParam
    @PostMapping("/findShippingAddresss")
    public PageResultDTO findShippingAddresss(@RequestBody PageDTO pageDTO) {
        return this.shippingAddressService.findShippingAddresss(pageDTO);
    }

    /**
     * 根据ID查询指定的收获地址
     *
     * @param shippingAddressId Id
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:view')")
    @ApiOperation("根据id查询收获地址")
    @ApiPageParam
    @PostMapping("/findShippingAddress")
    public ShippingAddress findShippingAddress(@RequestParam Long shippingAddressId) {
        return this.shippingAddressService.findShippingAddress(shippingAddressId);
    }

	@PreAuthorize("hasAuthority('mall:User:ShippingAddress:view')")
	@ApiOperation("根据userId查询收获地址")
	@ApiPageParam
	@PostMapping("/findShippingAddressByUserId")
	public List<ShippingAddress> findShippingAddressByUserId(@Param("userId") Long userId) {
		return this.shippingAddressService.findShippingAddressByUserId(userId);
	}

    /**
     * 根据ID查询指定的收获地址(包含外键名称)
     *
     * @param shippingAddressId Id
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:view')")
    @ApiOperation("根据ID查询指定的收获地址(包含外键名称)")
    @PostMapping("/findShippingAddressForView")
    public ShippingAddress findShippingAddressForView(@RequestParam Long shippingAddressId) {
        return this.shippingAddressService.findShippingAddressWithForeignName(shippingAddressId);
    }

    /**
     * 根据ID查询指定的收获地址(包含收获地址和外键名称)
     *
     * @param shippingAddressId Id
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:view')")
    @ApiOperation("根据ID查询指定的收获地址(包含收获地址和外键名称)")
    @PostMapping("/findShippingAddressForEdit")
    public ShippingAddressEditDto findShippingAddressForEdit(@RequestParam Long shippingAddressId) {
        ShippingAddressEditDto shippingAddressEditDto = new ShippingAddressEditDto();
        shippingAddressEditDto.setShippingAddress(this.shippingAddressService.findShippingAddressWithForeignName(shippingAddressId));

        this.prepareShippingAddressEditDto(shippingAddressEditDto);

        return shippingAddressEditDto;
    }

    /**
     * 根据ID查询指定的收获地址(只提取ID 和 Name)
     *
     * @param shippingAddressId Id
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:view')")
    @ApiOperation("根据ID查询指定的收获地址(只提取ID 和 Name)")
    @PostMapping("/findShippingAddresssWithIdNameById")
    public ShippingAddress findShippingAddresssWithIdNameById(@RequestParam Long shippingAddressId) {
        return this.shippingAddressService.findShippingAddresssWithIdNameById(shippingAddressId);
    }

    /**
     * 根据名称查询收获地址集合(只提取ID 和 Name)
     *
     * @param shippingAddressName 名称
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:view')")
    @ApiOperation("根据名称查询收获地址集合(只提取ID 和 Name)")
    @PostMapping("/findShippingAddresssWithIdNameByName")
    public List<ShippingAddress> findShippingAddresssWithIdNameByName(@RequestParam String shippingAddressName) {
        return this.shippingAddressService.findShippingAddresssWithIdNameByName(shippingAddressName);
    }


    /**
     * 创建新的收获地址
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:add')")
    @ApiOperation("创建新的收获地址")
    @PostMapping("/createShippingAddress")
    public ShippingAddressEditDto createShippingAddress() {
        ShippingAddressEditDto shippingAddressEditDto = new ShippingAddressEditDto();
        shippingAddressEditDto.setShippingAddress(new ShippingAddress());

        this.prepareShippingAddressEditDto(shippingAddressEditDto);
        return shippingAddressEditDto;
    }

    private void prepareShippingAddressEditDto(ShippingAddressEditDto shippingAddressEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        shippingAddressEditDto.setUserUsers(this.userService.findAllUsersWithIdName());
    }

    /**
     * 新增保存收获地址
     *
     * @param shippingAddress 实体对象
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:add')")
    @ApiOperation("新增保存收获地址")
    @PostMapping("/saveShippingAddress")
    public ShippingAddress saveShippingAddress(@RequestBody ShippingAddress shippingAddress) {
        return this.shippingAddressService.saveShippingAddress(shippingAddress);
    }

    /**
     * 修改保存收获地址
     *
     * @param shippingAddress 实体对象
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:update')")
    @ApiOperation("修改保存收获地址")
    @PostMapping("/updateShippingAddress")
    public ShippingAddress updateShippingAddress(@RequestBody ShippingAddress shippingAddress) {
        return this.shippingAddressService.updateShippingAddress(shippingAddress);
    }

    /**
     * 根据ID删除收获地址
     *
     * @param shippingAddressId ID
     */
    @PreAuthorize("hasAuthority('mall:User:ShippingAddress:remove')")
    @ApiOperation("根据ID删除收获地址")
    @PostMapping("/deleteShippingAddress")
    public void deleteShippingAddress(@RequestParam Long shippingAddressId) {
        this.shippingAddressService.deleteShippingAddress(shippingAddressId);
    }
    @ApiOperation("修改默认地址")
    @PostMapping("/setDefaultAddress")
    public void setDefaultAddress(@RequestBody ShippingAddress shippingAddress) {
        this.shippingAddressService.setDefaultAddress(shippingAddress);
    }
    @ApiOperation("获取默认地址")
    @PostMapping("/getDefaultAddress")
    public ShippingAddress getDefaultAddress() {
       return this.shippingAddressService.getDefaultAddress();
    }

}

